package com.ggy.dao;
/**
 * @description TODO User实体数据访问接口
 * @author Michael
 * @createTime 2017年8月13日 下午11:50:43
 * 
 */

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ggy.dao.bean.UserBean;
import com.ggy.entity.User;
import com.ggy.utils.JdbcHelper;

public interface UserDao {
	JdbcHelper jdbc = new JdbcHelper();
	String BASE_SQL = "select * from sys_user";
	String BASE_SQL_USER_ROLE = "select u.*,r.role_name from sys_user u left join sys_role r on u.roleId=r.id";

	/**
	 * 根据条件查询User实体
	 * @param params
	 * @return
	 */
	List<UserBean> getUsersbyCondition(int page,int size,Map<Object, Object> params);
	/**
	 * 得到总记录数
	 * @param params
	 * @return
	 */
	int getTotalCount(Map<Object,Object> params);
	/**
	 * 封装User实体的字段映射
	 * @param rs
	 * @return
	 */
	default List<User> _getUsers(ResultSet rs) {
		List<User> _list = new ArrayList<User>();
		try {
			while (rs.next()) {
				User user = new User();
				user.setCreateBy(rs.getString("createby"));
				user.setCreateTime(rs.getString("createtime"));
				user.setEmail(rs.getString("email"));
				user.setId(rs.getInt("id"));
				user.setLoginName(rs.getString("loginname"));
				user.setLoginPwd(rs.getString("loginpwd"));
				user.setModifyBy(rs.getString("modifyby"));
				user.setModifyTime(rs.getString("modifytime"));
				user.setRoleId(rs.getInt("roleid"));
				user.setRealName(rs.getString("realname"));
				user.setTel(rs.getString("tel"));
				user.setVersion(rs.getInt("version"));
				_list.add(user);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			jdbc.relese();
		}
		return _list;
	}
	/**
	 * 封装User实体的字段映射
	 * @param rs
	 * @return
	 */
	default List<UserBean> _getUserBeans(ResultSet rs) {
		List<UserBean> _list = new ArrayList<UserBean>();
		try {
			while (rs.next()) {
				UserBean user = new UserBean();
				user.setCreateBy(rs.getString("createby"));
				user.setCreateTime(rs.getString("createtime"));
				user.setEmail(rs.getString("email"));
				user.setId(rs.getInt("id"));
				user.setLoginName(rs.getString("loginname"));
				user.setLoginPwd(rs.getString("loginpwd"));
				user.setModifyBy(rs.getString("modifyby"));
				user.setModifyTime(rs.getString("modifytime"));
				user.setRoleId(rs.getInt("roleid"));
				user.setTel(rs.getString("tel"));
				user.setRealName(rs.getString("realName"));
				user.setRoleName(rs.getString("role_name"));
				_list.add(user);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			jdbc.relese();
		}
		return _list;
	}
	/**
	 * 构造查询条件
	 * @param userBean
	 * @param params
	 * @return
	 */
	default String whereStr(Map<Object, Object> params) {
		String str="";
		if(params!=null) {
			str+=" where 1=1 ";
			for(Object obj:params.keySet()) {
				if(obj instanceof String) {
					str+=" and "+obj+" like '%"+params.get(obj)+"%'";
				}else {
					str+=" and "+obj+"="+params.get(obj);
				}
			}
		}
		return str;
	}
}
